package com.changanw.tienchin.web.controller.tienchin;

import com.changanw.tienchin.channel.domain.Channel;
import com.changanw.tienchin.channel.domain.vo.ChannelVo;
import com.changanw.tienchin.channel.service.IChannelService;
import com.changanw.tienchin.common.annotation.Log;
import com.changanw.tienchin.common.core.controller.BaseController;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.core.page.TableDataInfo;
import com.changanw.tienchin.common.enums.BusinessType;
import com.changanw.tienchin.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author changanw
 * @since 2023-11-15
 */
@RestController
@RequestMapping("/tienchin/channel")
public class ChannelController extends BaseController {
  @Autowired
  IChannelService channelService;
  @PreAuthorize("hasPermission('tienchin:channel:list')")
  @GetMapping("/list")
  public TableDataInfo list(ChannelVo channelVo) {
    startPage();
    List<Channel> list = channelService.selectChannelList(channelVo);
    return getDataTable(list);
  }
  /**
   * 新增渠道
   */
  @PreAuthorize("hasPermission('tienchin:channel:create')")
  @Log(title = "渠道管理", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody ChannelVo channelVo) {
    return channelService.addChannel(channelVo);
  }
  /**
   * 修改渠道
   */
  @PreAuthorize("hasPermission('tienchin:channel:edit')")
  @Log(title = "渠道管理", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody ChannelVo channelVo) {
    return channelService.updateChannel(channelVo);
  }
  @PreAuthorize("hasPermission('tienchin:channel:list')")
  @GetMapping("/{channelId}")
  public AjaxResult getInfo(@PathVariable Long channelId){
    return AjaxResult.success(channelService.getById(channelId));
  }
  /**
   * 删除渠道
   */
  @PreAuthorize("hasPermission('tienchin:channel:remove')")
  @Log(title = "渠道管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/{channelIds}")
  public AjaxResult remove(@PathVariable Long[] channelIds) {
    return toAjax(channelService.deleteChannelByIds(channelIds));
  }

  /**
   * 渠道导出
   * @param response
   * @param role
   */
  @Log(title = "渠道管理", businessType = BusinessType.EXPORT)
  @PreAuthorize("hasPermission('tienchin:channel:export')")
  @PostMapping("/export")
  public void export(HttpServletResponse response, ChannelVo channelVo) {
    List<Channel> list = channelService.selectChannelList(channelVo);
    ExcelUtil<Channel> util = new ExcelUtil<Channel>(Channel.class);
    util.exportExcel(response, list, "渠道数据");
  }

  /***
   * 下载导入数据的模板
   * @param response
   */
  @PostMapping("/importTemplate")
  public void importTemplate(HttpServletResponse response) {
    ExcelUtil<Channel> util = new ExcelUtil<Channel>(Channel.class);
    util.importTemplateExcel(response, "渠道数据");
  }

  /**
   * 渠道导入
   * @param file
   * @param updateSupport
   * @return
   * @throws Exception
   */
  @Log(title = "渠道管理", businessType = BusinessType.IMPORT)
  @PreAuthorize("hasPermission('tienchin:channel:import')")
  @PostMapping("/importData")
  public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
    ExcelUtil<Channel> util = new ExcelUtil<Channel>(Channel.class);
    List<Channel> channelList = util.importExcel(file.getInputStream());
    String message = channelService.importUser(channelList, updateSupport);
    return success(message);
  }
}
